import numpy
import math
import scipy
import scipy.io
import scipy.misc
import scipy.cluster
import scipy.cluster.vq
import matplotlib
import matplotlib.pyplot
import os
import IPython
import IPython.parallel
import itertools
import random
import sklearn
import sklearn.decomposition
import sklearn.manifold
import sklearn.cluster
import sklearn.feature_extraction
import sklearn.feature_extraction.text
base_path = "/u/mlrobert/code/local/2013_drawing_assistant/data/"
filter_responses_base_path = \
base_path+"filter_responses/bunny2/apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08/"
local_features_base_path = \
base_path+"local_features/bunny2/"+\
"apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08.galif.patch_width=15.num_samples=32.num_tiles=04/"
debug_base_path = \
base_path+"debug/local_features/bunny2/"+\
"apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08.galif.patch_width=15.num_samples=32.num_tiles=04/"
def compute_galif_local_patch_features(img_name):
import numpy
import scipy
import scipy.io
import scipy.misc
import matplotlib
import matplotlib.pyplot
import os
import itertools
patch_width_as_fraction_of_width = 0.15
num_samples_width = 32
num_tiles_per_dimension_per_patch = 4
use_squared_filter_response = True
filter_responses_names_exts = sorted(os.listdir(filter_responses_base_path+img_name))
for filter_response_name_ext in filter_responses_names_exts:
filter_response_name = filter_response_name_ext[:-4]
filter_response_ext = filter_response_name_ext[-4:]
local_features_path = local_features_base_path+img_name+"/"+filter_response_name+"/"
debug_path = debug_base_path+img_name+"/"+filter_response_name+"/"
if not os.path.exists(local_features_path):
os.makedirs(local_features_path)
if not os.path.exists(debug_path):
os.makedirs(debug_path)
filter_response_mat = scipy.io.loadmat(filter_responses_base_path+img_name+"/"+filter_response_name_ext)
filter_response = filter_response_mat["filter_response"]
num_samples_height = (float(filter_response.shape[0])*float(num_samples_width))/float(filter_response.shape[1])
samples_y = numpy.linspace(0, filter_response.shape[0]-1, num_samples_height+2).astype(numpy.int32)
samples_x = numpy.linspace(0, filter_response.shape[1]-1, num_samples_width+2).astype(numpy.int32)
patch_size = int(filter_response.shape[1] * patch_width_as_fraction_of_width)
if patch_size % 2 == 0:
patch_size = patch_size + 1
patch_half_size = (patch_size - 1) / 2
if use_squared_filter_response:
filter_response_to_sample = abs(filter_response)*abs(filter_response)
else:
filter_response_to_sample = abs(filter_response)
filter_response_to_sample_padded = numpy.pad(filter_response_to_sample, (patch_half_size,patch_half_size), "constant")
for sample in list(itertools.product(samples_y, samples_x)):
patch = filter_response_to_sample_padded[sample[0]:sample[0]+patch_size, sample[1]:sample[1]+patch_size]
tile_bounds = numpy.linspace(0, patch_size, num_tiles_per_dimension_per_patch+1).astype(numpy.int32)
tile = numpy.zeros((num_tiles_per_dimension_per_patch, num_tiles_per_dimension_per_patch), dtype=numpy.float32)
for tile_index in list(itertools.product(range(num_tiles_per_dimension_per_patch), range(num_tiles_per_dimension_per_patch))):
tile[tile_index] = numpy.mean(patch[tile_bounds[tile_index[0]]:tile_bounds[tile_index[0]+1], \
tile_bounds[tile_index[1]]:tile_bounds[tile_index[1]+1]])
scipy.io.savemat(
"%sy=%04d.x=%04d.mat" % (local_features_path, sample[0], sample[1]),
{"tile":tile},
oned_as="column")
matplotlib.pyplot.clf();
matplotlib.pyplot.imshow(tile, interpolation="nearest");
matplotlib.pyplot.colorbar();
matplotlib.pyplot.savefig("%sy=%04d.x=%04d.png" % (debug_path, sample[0], sample[1]));
matplotlib.pyplot.clf();
img_names = sorted(os.listdir(filter_responses_base_path))
compute_galif_local_patch_features(img_names[0])
client = IPython.parallel.Client()
directView = client[:]
directView["filter_responses_base_path"] = filter_responses_base_path
directView["local_features_base_path"] = local_features_base_path
directView["debug_base_path"] = debug_base_path
asyncResult = directView.map_async(compute_galif_local_patch_features, img_names)
print asyncResult.progress
print asyncResult.ready()
32 True
result = asyncResult.get()
print result
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]